package com.gh.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gh.common.exception.Assert;
import com.gh.common.result.ResponseEnum;
import com.gh.entity.UserBind;
import com.gh.entity.UserInfo;
import com.gh.enums.UserBindEnum;
import com.gh.hfb.FormHelper;
import com.gh.hfb.HfbConst;
import com.gh.hfb.RequestHelper;
import com.gh.mapper.UserBindMapper;
import com.gh.mapper.UserInfoMapper;
import com.gh.service.UserBindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gh.vo.UserBindVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author Eric
 * @since 2021-09-06
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public String commitUserBind(UserBindVo userBindVo, Long userId) {
        //不同的userId,相同的身份证,如果存在,则不允许
        LambdaQueryWrapper<UserBind> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserBind::getIdCard, userBindVo.getIdCard())
                .ne(UserBind::getUserId, userId);
        UserBind userBind = baseMapper.selectOne(lambdaQueryWrapper);
        Assert.isNull(userBind, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);
        //判断用户是否已绑定
        lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserBind::getUserId, userId);
        userBind = baseMapper.selectOne(lambdaQueryWrapper);
        if (ObjectUtils.isEmpty(userBind)) {
            //创建用户绑定记录
            userBind = new UserBind();
            BeanUtils.copyProperties(userBindVo, userBind);
            userBind.setUserId(userId);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            baseMapper.insert(userBind);
        } else {
            //如果已经绑定就修改
            BeanUtils.copyProperties(userBindVo, userBind);
            baseMapper.updateById(userBind);
        }
        //组装自动提交表单参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("agentId", HfbConst.AGENT_ID);
        paramMap.put("agentUserId", userId);
        paramMap.put("idCard", userBindVo.getIdCard());
        paramMap.put("personalName", userBindVo.getName());
        paramMap.put("bankType", userBindVo.getBankType());
        paramMap.put("bankNo", userBindVo.getBankNo());
        paramMap.put("mobile", userBindVo.getMobile());
        paramMap.put("returnUrl", HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        paramMap.put("sign", RequestHelper.getSign(paramMap));
        //动态生成表单字符串
        String formStr = FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);
        return formStr;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void notifyBind(Map<String, Object> map) {
        String bindCode = (String) map.get("bindCode");//获取绑定码
        String userId = (String) map.get("agentUserId");//用户id
        //更新绑定表
        LambdaQueryWrapper<UserBind> userBindLambdaQueryWrapper=new LambdaQueryWrapper<>();
        userBindLambdaQueryWrapper.eq(UserBind::getUserId,userId);
        UserBind userBind = baseMapper.selectOne(userBindLambdaQueryWrapper);
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        userBind.setBindCode(bindCode);
        baseMapper.updateById(userBind);
        //更新用户表
        //通过userID查询用户信息
        UserInfo userInfo = userInfoMapper.selectById(userId);
        userInfo.setBindCode(bindCode);
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public String getBindCodeByUserId(Long userId) {
        LambdaQueryWrapper<UserBind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBind::getUserId,userId);
        UserBind userBind = baseMapper.selectOne(queryWrapper);
        return userBind.getBindCode();
    }
}
